package leetcode_core.leetcode_2;

import javax.jws.Oneway;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

//元素可重不可复选
public class PermuteUnique {

    private List<List<Integer>> ans = new ArrayList<>();
    private boolean[] book = new boolean[10];

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        backtrack(nums,new LinkedList<>(),0);
        return ans;
    }

    private void backtrack(int[] nums,LinkedList<Integer> track,int start){
        if(track.size() == nums.length){
            ans.add(new ArrayList<>(track));
            return;
        }
        for(int i = start;i<nums.length;i++){
            if(i>0 && nums[i] == nums[i-1] && !book[i-1]){
                continue;
            }
            //做选择
            track.add(nums[i]);
            book[i] = true;
            //回溯递归
            backtrack(nums,track,i+1);
            //撤销选择
            track.removeLast();
            book[i] = false;
        }
    }






}
